package com.xbang.attendance.web.action.system.warehouseproduct;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.system.Warehouse;
import com.xbang.attendance.entity.system.WarehouseProductAgreement;
import com.xbang.attendance.service.SystemService;
import com.xbang.attendance.service.WarehouseService;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.page.Result;
import com.xbang.core.struts.support.SearchActionSupport;

public class Index extends SearchActionSupport {

	private static final long serialVersionUID = -3985569449034035768L;

	private static final Log logger = LogFactory.getLog(Index.class);
	
	@Autowired
	private SystemService systemService;
	private String type;
	private Integer warehouseId; 
	private String name;
	private String code;
	private List<Warehouse> warehouses = new ArrayList<Warehouse>();
	private Warehouse warehouse;
	@Autowired
	private WarehouseService warehouseService;
	private List<WarehouseProductAgreement> warehouseProductAgreement = new ArrayList<WarehouseProductAgreement>();
	
	@Action("/system/warehouseproduct/list")
	public String list() throws Exception {

		///'//searchComponent.getOrderParameters().clear();
		searchComponent.resetWhereSentence();
		searchComponent.resetOrderSentence();
		searchComponent.resetGroupSentence();
		String hqlSentence = "FROM Warehouse warehouse WHERE 1 = 1";
		
		if(StringUtils.isNotEmpty(name)){
			searchComponent.addSearchElement("like.warehouse.name", name);
		}
		if(StringUtils.isNotEmpty(code)){
			searchComponent.addSearchElement("eq.warehouse.code", code);
		}
		
		if (searchComponent.getOrderParameters().isEmpty()){
			searchComponent.addSearchElement("orderBy.warehouse.code", "ASC");
			//searchComponent.addSearchElement("groupBy.warehouseproductagreement.warehouse.code","");
		}
		Result result = systemService.searchByPage(hqlSentence, searchComponent, page);

		warehouses = result.getItems();
		
		page = result.getPage();

		logger.info(">>>>>>>>>>>>");
		return "index";
	}
	
	@Action("/system/warehouseproduct/export")
	public String export() throws Exception{
		try{
			String exportFile = "";
			String webPath = ServletActionContext.getRequest().getRealPath("//download//warehouse//product");
			HttpServletRequest request = ServletActionContext.getRequest();
			String urlPath = request.getRequestURL().toString();
			String contextPath = request.getContextPath();
			String contextURL = urlPath.split(contextPath)[0] + contextPath;
			exportFile = contextURL + "/download/warehouse/product/" + warehouseService.genericExcelFile(webPath,type);
			//System.out.println(exportFile);
			return "r:" + exportFile;
		}catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	
	
	@Override
	public void resetCustomerSearchCriteria() {
		

	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public SystemService getSystemService() {
		return systemService;
	}

	public void setSystemService(SystemService systemService) {
		this.systemService = systemService;
	}

	public Integer getWarehouseId() {
		return warehouseId;
	}

	public void setWarehouseId(Integer warehouseId) {
		this.warehouseId = warehouseId;
	}

	public List<Warehouse> getWarehouses() {
		return warehouses;
	}

	public void setWarehouses(List<Warehouse> warehouses) {
		this.warehouses = warehouses;
	}

	public Warehouse getWarehouse() {
		return warehouse;
	}

	public void setWarehouse(Warehouse warehouse) {
		this.warehouse = warehouse;
	}

	public List<WarehouseProductAgreement> getWarehouseProductAgreement() {
		return warehouseProductAgreement;
	}

	public void setWarehouseProductAgreement(
			List<WarehouseProductAgreement> warehouseProductAgreement) {
		this.warehouseProductAgreement = warehouseProductAgreement;
	}





	public String getName() {
		return name;
	}





	public void setName(String name) {
		this.name = name;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}

	



	 

}
